use crate::component::request::Request;
use crate::component::response::Response;
use ureq;

/// 下载中间件处理 request 和 response 时的返回结果
/// 用于指明后续处理的方式
pub enum DownloaderMwResult<ItemType: Clone> {
    /// 停止处理该请求, 不再继续调用 `process_request()`, 重新调度该 request.
    FinalRequest(Request<ItemType>),
    /// 继续处理该请求, 直到最后交给下载器发送该 request
    IntermediateRequest(Request<ItemType>),
    /// 不再调用任何的 `process_request()`, `process_response()`, 或者下载;
    /// 而是直接返回该 response
    Response(Response<ItemType>),
    /// TODO: 这里?
    Ignore,
}

/// 指定当下载发生下载错误时如何处理
pub enum DownloaderMwErrResult<ItemType: Clone> {
    /// 继续调用 `process_error()` 处理
    Continue,
    Request(Request<ItemType>),
    Response(Response<ItemType>),
}

pub trait DownloaderMiddleware<ItemType: Clone>: Send {
    fn process_request(&mut self, request: Request<ItemType>) -> DownloaderMwResult<ItemType>;
    fn process_response(
        &mut self,
        request: &Request<ItemType>,
        response: Response<ItemType>,
    ) -> DownloaderMwResult<ItemType>;
    fn process_error(
        &mut self,
        request: &Request<ItemType>,
        error: &ureq::Error,
    ) -> DownloaderMwErrResult<ItemType>;
}
